﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.dal;

namespace SQLEditor.admin.service
{
    public class ProgQuestionService:BaseService
    {
        public string getall()
        {
            var list = from b in DB.progQuestion

                       select new ProgQuestion
                       {
                           id = b.id,
                           title = b.title,
                           remark = b.remark,
                           input = b.input,
                           output = b.output,
                           sorter = b.sorter,
                           userId = b.userId,    
                           point = b.point                          
                       };
            string json = JsonUtil.ToJson(list.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where)
        {
            int totalCount = 0;
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.progQuestion.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("progQuestion", "*", order, where, pageSize, pageIndex, paramTotalCount)).ToList();
            var list = from b in pager

                       select new ProgQuestion
                       {
                           id = b.id,
                           title = b.title,
                           remark = b.remark,
                           input = b.input,
                           output = b.output,
                           sorter = b.sorter,
                           userId = b.userId,
                           point = b.point
                           
                       };
            var l = list.ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string add(progQuestion b)
        {
            DB.progQuestion.Add(b);
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success = true });
            return json;
        }

        public string edit(progQuestion b)
        {
            var old = DB.progQuestion.FirstOrDefault(m => m.id == b.id);
            old.title = b.title;
            old.remark = b.remark;
            old.output = b.output;
            old.input = b.input;
            old.sorter = b.sorter;
            old.point = b.point;
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success = true });
            return json;
        }

        public string delete(string strIds)
        {
            if (string.IsNullOrEmpty(strIds))
            {
                return JsonUtil.ToJson(new { success = false, msg = "没有选择要删除的记录！" });
            }
            string[] arr = strIds.Split(',');
            List<int> ids = new List<int>();
            foreach (var id in arr)
            {
                ids.Add(int.Parse(id));
            }
            var questions = DB.progQuestion.Where(m => ids.Contains(m.id)).ToList();
            if (questions != null)
            {
                questions.ForEach(m => DB.progQuestion.Remove(m));
                DB.SaveChanges();
                return JsonUtil.ToJson(new { success = true });
            }
            else
            {
                return JsonUtil.ToJson(new { success = false, msg = "删除失败！" });
            }
        }       
    }
}